# -*- coding: utf-8 -*-
# @Time    : 2022/3/16 2:19 下午
# @Author  : zlt
# @FileName: main.py
# @Software: PyCharm

import os,shutil,pytest
from common import util_path,util_log

Log = util_log.Logger()

if __name__ == '__main__':
    #第一步，格式化测试报告文件夹
    if os.path.exists(util_path.BASE_PATH):
        #清空测试报告文件夹
        shutil.rmtree(util_path.REPORT_PATH)
        #新建测试报告文件夹
        os.mkdir(util_path.REPORT_PATH)
        Log.info("初始化测试报告文件夹成功！")
    else:
        Log.info("初始化测试报告文件夹失败！")
        pass

    #第二步，开始执行需要执行的测试用例
    Log.info("开始执行测试用例")
    try:
        pytest.main(["-s","-v",util_path.TESTCASE_PATH,"--alluredir",util_path.REPORT_PATH])
        Log.info("用例执行完毕！")
    except BaseException as a:
        Log.info("测试用例执行异常信息：",a)

    #第三步，生成本地测试结果报告
    Log.info("开始生成测试结果报告")
    try:
        os.system("allure generate --clean {} -o {}".format(util_path.REPORT_PATH,util_path.RESULT_PATH))
        Log.info("测试结果报告生成成功！")
    except Exception as e:
        Log.info("测试结果生成报告异常信息：",e)

    #第四步打开本地的allure测试报告链接
    os.system("allure open -h 127.0.0.1 -p 8888 {}".format(util_path.RESULT_PATH))



